using Godot;

namespace AndroidUnevenRoadGame.Game
{
    public sealed class EnemyState
    {
        private readonly int _minDamage;
        private readonly int _maxDamage;

        public EnemyDefinition Definition { get; }
        public int MaxHp { get; }
        public int CurrentHp { get; private set; }
        public bool IsDead => CurrentHp <= 0;
        public int MinDamage => _minDamage;
        public int MaxDamage => _maxDamage;

        public EnemyState(EnemyDefinition definition, int floor)
        {
            Definition = definition;

            var effectiveFloor = Mathf.Max(1, floor);
            var hpScale = Mathf.Pow(1f + definition.HpGrowthPerFloor, effectiveFloor - 1);
            var damageScale = Mathf.Pow(1f + definition.DamageGrowthPerFloor, effectiveFloor - 1);

            MaxHp = Mathf.Max(1, Mathf.RoundToInt(definition.BaseMaxHp * hpScale));
            CurrentHp = MaxHp;

            _minDamage = Mathf.Max(0, Mathf.RoundToInt(definition.BaseMinDamage * damageScale));
            _maxDamage = Mathf.Max(_minDamage, Mathf.RoundToInt(definition.BaseMaxDamage * damageScale));
        }

        public void TakeDamage(int amount)
        {
            CurrentHp = Mathf.Max(0, CurrentHp - amount);
        }

        public int RollAttackDamage()
        {
            var rng = GameRoot.Instance.Random;
            return rng.RandiRange(_minDamage, _maxDamage);
        }
    }
}

